package com.otp.controller;

import com.otp.pojo.dto.PaperDto;
import com.otp.pojo.dto.PaperSubmissionDto;
import com.otp.pojo.entity.Paper;
import com.otp.pojo.entity.PaperSubmission;
import com.otp.pojo.request.paper.*;
import com.otp.util.ResponseMessage;
import com.otp.service.PaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/paper")
public class PaperController {
    @Autowired
    private PaperService paperService;

    @GetMapping
    public ResponseMessage<List<PaperDto>> list() {
        return ResponseMessage.success(paperService.list()
                .stream()
                .map(Paper::convertToDto)
                .toList());
    }

    @GetMapping("/status/{status}")
    public ResponseMessage<List<PaperDto>> listByStatus(@PathVariable String status) {
        return ResponseMessage.success(paperService.listByStatus(status)
                .stream()
                .map(Paper::convertToDto)
                .toList());
    }

    @GetMapping("/{id}")
    public ResponseMessage<PaperDto> getById(@PathVariable Long id) {
        return ResponseMessage.success(paperService.getById(id)
                .convertToDto());
    }

    @PostMapping
    public ResponseMessage<PaperDto> create(@RequestBody PaperCreateRequest request) {
        return ResponseMessage.success(paperService.create(request)
                .convertToDto());
    }

    @PutMapping
    public ResponseMessage<PaperDto> update(@RequestBody PaperUpdateRequest request) {
        return ResponseMessage.success(paperService.update(request)
                .convertToDto());
    }

    @PostMapping("/publish/{id}")
    public ResponseMessage<PaperDto> publish(@PathVariable Long id) {
        return ResponseMessage.success(paperService.publish(id)
                .convertToDto());
    }

    @DeleteMapping("/{id}")
    public ResponseMessage<?> delete(@PathVariable Long id) {
        paperService.delete(id);
        return ResponseMessage.success(null);
    }

    @PostMapping("/submit")
    public ResponseMessage<PaperSubmissionDto> submit(@RequestBody PaperSubmissionRequest request) {
        return ResponseMessage.success(paperService.submit(request)
                .convertToDto());
    }
}